package com.yuheadline.article.controller;

import com.dust.yuheadline.common.utils.Result;
import com.yuheadline.article.service.ArticleImageService;
import com.yuheadline.model.article.dto.SaveArticleImgDto;
import com.yuheadline.model.article.vo.SaveArticleImageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 高翔宇
 * @since 2025/4/19 周六 17:47
 */
@Slf4j
@RestController
@RequestMapping("/api/article/articleImg")
@RequiredArgsConstructor
@Tag(name = "文章管理接口")
public class ArticleImageController {
    private final ArticleImageService articleImageService;

    /**
     * 保存文章图片（正确写法）
     */
    @PostMapping("/upload")
    @Operation(summary = "上传图片")
    public Result<SaveArticleImageVo> saveArticleImage(
            @RequestParam("articleImage") @Validated MultipartFile file,  // 文件字段
            @ModelAttribute SaveArticleImgDto saveArticleImgDto           // 表单普通字段（替代 @RequestBody）
    ) {
        log.info("保存图片：{}，请求体：{}", file, saveArticleImgDto);
        return articleImageService.saveArticleImage(file, saveArticleImgDto);
    }
    /**
     * 保存文章图片，接受url
     */
    @PostMapping("/saveUrl")
    @Operation(summary = "保存图片url")
    public Result<SaveArticleImageVo> saveArticleImageUrl(@ModelAttribute SaveArticleImgDto saveArticleImgDto) {
        log.info("保存图片url：{}", saveArticleImgDto);
        return articleImageService.saveArticleImageUrl(saveArticleImgDto);
    }
}
